package com.t3rik.mes.emp.controller;

import java.util.List;
import java.util.Map;

import com.t3rik.mes.emp.domain.EmpAttendanceRecord;
import com.t3rik.mes.emp.service.IEmpAttendanceRecordService;
import jakarta.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.t3rik.common.annotation.Log;
import com.t3rik.common.core.controller.BaseController;
import com.t3rik.common.core.domain.AjaxResult;
import com.t3rik.common.utils.StringUtils;
import com.t3rik.common.enums.BusinessType;

import com.t3rik.common.utils.poi.ExcelUtil;
import com.t3rik.common.core.page.TableDataInfo;

/**
 * 员工考勤记录（手动记录）Controller
 *
 * @author ykh
 * @date 2025-02-18
 */
@RestController
@RequestMapping("/sm/hrm-employment-kaoqin")
public class EmpAttendanceRecordController extends BaseController {
    @Autowired
    private IEmpAttendanceRecordService empAttendanceRecordService;

    /**
     * 查询员工考勤记录（手动记录）列表
     */
    @PreAuthorize("@ss.hasPermi('md:record:list')")
    @GetMapping("/list")
    public TableDataInfo list(EmpAttendanceRecord empAttendanceRecord) {
        // 获取查询条件
        LambdaQueryWrapper<EmpAttendanceRecord> queryWrapper = getQueryWrapper(empAttendanceRecord);
        // 组装分页
        Page<EmpAttendanceRecord> page = getMPPage(empAttendanceRecord);
        // 查询
        this.empAttendanceRecordService.page(page, queryWrapper);
        return getDataTableWithPage(page);
    }

    /**
     * 导出员工考勤记录（手动记录）列表
     */
    @PreAuthorize("@ss.hasPermi('md:record:export')")
    @Log(title = "员工考勤记录（手动记录）", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EmpAttendanceRecord empAttendanceRecord) {
        // 获取查询条件
        LambdaQueryWrapper<EmpAttendanceRecord> queryWrapper = getQueryWrapper(empAttendanceRecord);
        List<EmpAttendanceRecord> list = this.empAttendanceRecordService.list(queryWrapper);
        ExcelUtil<EmpAttendanceRecord> util = new ExcelUtil<EmpAttendanceRecord>(EmpAttendanceRecord. class);
        util.exportExcel(response, list, "员工考勤记录（手动记录）数据");
    }

    /**
     * 获取员工考勤记录（手动记录）详细信息
     */
    @PreAuthorize("@ss.hasPermi('md:record:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(this.empAttendanceRecordService.getById(id));
    }

    /**
     * 新增员工考勤记录（手动记录）
     */
    @PreAuthorize("@ss.hasPermi('md:record:add')")
    @Log(title = "员工考勤记录（手动记录）", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EmpAttendanceRecord empAttendanceRecord) {
        return toAjax(this.empAttendanceRecordService.save(empAttendanceRecord));
    }

    /**
     * 修改员工考勤记录（手动记录）
     */
    @PreAuthorize("@ss.hasPermi('md:record:edit')")
    @Log(title = "员工考勤记录（手动记录）", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EmpAttendanceRecord empAttendanceRecord) {
        return toAjax(this.empAttendanceRecordService.updateById(empAttendanceRecord));
    }

    /**
     * 删除员工考勤记录（手动记录）
     */
    @PreAuthorize("@ss.hasPermi('md:record:remove')")
    @Log(title = "员工考勤记录（手动记录）", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        return toAjax(this.empAttendanceRecordService.removeByIds(ids));
    }

    /**
    * 获取查询条件
    */
    public LambdaQueryWrapper<EmpAttendanceRecord> getQueryWrapper(EmpAttendanceRecord empAttendanceRecord) {
        LambdaQueryWrapper<EmpAttendanceRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(empAttendanceRecord.getUserId() != null, EmpAttendanceRecord::getUserId, empAttendanceRecord.getUserId());
        queryWrapper.like(StringUtils.isNotEmpty(empAttendanceRecord.getUserName()), EmpAttendanceRecord::getUserName, empAttendanceRecord.getUserName());
        queryWrapper.eq(empAttendanceRecord.getDayCount() != null, EmpAttendanceRecord::getDayCount, empAttendanceRecord.getDayCount());
        queryWrapper.eq(empAttendanceRecord.getNightCount() != null, EmpAttendanceRecord::getNightCount, empAttendanceRecord.getNightCount());
        queryWrapper.eq(empAttendanceRecord.getDeleted() != null, EmpAttendanceRecord::getDeleted, empAttendanceRecord.getDeleted());
        queryWrapper.eq(empAttendanceRecord.getDeleteAt() != null, EmpAttendanceRecord::getDeleteAt, empAttendanceRecord.getDeleteAt());
        // 默认创建时间倒序
        queryWrapper.orderByDesc(EmpAttendanceRecord::getCreateTime);
        Map<String, Object> params = empAttendanceRecord.getParams();
        queryWrapper.between(params.get("beginTime") != null && params.get("endTime") != null,EmpAttendanceRecord::getCreateTime, params.get("beginTime"), params.get("endTime"));
        return queryWrapper;
    }
}
